//
//  JZHZBRViewController.m
//  极致荟
//
//  Created by hetao on 14-8-19.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHZBRViewController.h"
#import "JZHFBViewController.h"
@interface JZHZBRViewController ()
{
    UIView *av;
    BOOL animd;
}
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *hud;

@end

@implementation JZHZBRViewController
ZBarReaderView *readerView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
     [self remload];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title=@"扫一扫";
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
   
    
}

- (void)remload
{
    [self.hud startAnimating];
    self.hud.hidden=NO;
    readerView = [[ZBarReaderView alloc]init];
    
    readerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    readerView.readerDelegate = self;
    //关闭闪光灯
    readerView.torchMode = 0;
    //扫描区域
    animd=NO;
    //处理模拟器
    
    if (TARGET_IPHONE_SIMULATOR) {
        
        ZBarCameraSimulator *cameraSimulator
        
        = [[ZBarCameraSimulator alloc]initWithViewController:self];
        
        cameraSimulator.readerView = readerView;
        
    }
    
    [self.view addSubview:readerView];
    CGRect scanMaskRect = CGRectMake(60, 200,200, 200);
    
    //[self.view addSubview:view];
    //扫描区域计算
    
    readerView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:readerView.bounds];
    
    [readerView addSubview:self.evmview];
    
    [readerView start];
    [self performSelector:@selector(amian) withObject:self afterDelay:1.5];
    // [self amian];

}
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{
    CGFloat x,y,width,height;
    x = rect.origin.x / readerViewBounds.size.width;
    y = rect.origin.y / readerViewBounds.size.height;
    width = rect.size.width / readerViewBounds.size.width;
    height = rect.size.height / readerViewBounds.size.height;
    return CGRectMake(x, y, width, height);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)leftDrawerButtonPress:(UIButton *)sender
{
    animd=YES;
    [readerView stop];
    [av removeFromSuperview];
    [UINavigationController Pop:self];
}


#pragma mark----二维码扫描代理----
- (void)amian
{
    [self.hud stopAnimating];
    self.hud.hidden=YES;
    if(animd)
    {
        return;
    }
    if(!av)
    {
        av=[[UIView alloc] initWithFrame:CGRectMake(60, self.topevm.frame.origin.y, 200, 1)];
        [av setBackgroundColor:[UIColor blueColor]];
        [self.view addSubview:av];
    }
    
    [UIView animateWithDuration:3 animations:^{
        [av setFrame: CGRectMake(60, av.frame.origin.y+self.topevm.frame.size.height, 200, 1)];
    } completion:^(BOOL finished) {
        [av setFrame: CGRectMake(60, self.topevm.frame.origin.y, 200, 1)];
        [self amian];
        
    }];
}

- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
    for (ZBarSymbol *symbol in symbols) {
        
        NSLog(@"%@", symbol.data);
        NSString *str=[symbol.data substringWithRange:NSMakeRange(0, 6)];
        
        if([str isEqualToString:@"userId"])
        {
            NSLog(@"%@",[symbol.data substringFromIndex:7]);
            [self pususerData:[symbol.data substringFromIndex:7]];
        }else
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:symbol.data]];
        }
        break;
    }
    animd=YES;
    [readerView stop];
    [av removeFromSuperview];
    //[UINavigationController Pop:self];
}

- (void)pususerData:(NSString *)uid
{
    self.hud.hidden=NO;
    [self.hud startAnimating];
    
    NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:uid,@"userId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"member/getMemberInfo") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            NSLog(@"%@",data);
            animd=NO;
            av=nil;
            NSDictionary *data1=[data objectForKey:@"data"];
            OtherUsers *ouser=[[OtherUsers alloc] init];
            ouser.userId=[[data1 objectForKey:@"userId"] intValue];
            ouser.name=[data1 objectForKey:@"Nickname"];
            if([data1 objectForKey:@"Sex"]==[NSNull null])
            {
                ouser.sex=1;
            }else
            {
                ouser.sex=[[data1 objectForKey:@"Sex"] intValue];
            }
            
            ouser.isStr=[data1 objectForKey:@"Sign"];
            ouser.userHeadImage=[[data1 objectForKey:@"imgPrefix"] stringByAppendingString:[data1 objectForKey:@"Avatar"]];
            ouser.vip=[[data1 objectForKey:@"VIP"] intValue];
            if([data1 objectForKey:@"City"]==[NSNull null])
            {
             ouser.locationdic=[[NSDictionary alloc] initWithObjectsAndKeys:@"暂无",@"cityName", nil];
            }else
            {
             ouser.locationdic=[[NSDictionary alloc] initWithObjectsAndKeys:[data1 objectForKey:@"City"],@"cityName", nil];
            }
           
            [self.hud stopAnimating];
            self.hud.hidden=YES;
            [self.navigationController pushViewController:[[JZHFBViewController alloc] initOuser:ouser] animated:YES];
        }
    }];
}


@end
